﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FBNotifier
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        private string loginUrl = String.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri=http://www.facebook.com/connect/login_success.html&type=user_agent&display=popup&scope=offline_access,read_mailbox", "112887725434899");

        static public event EventHandler UserLoggedIn;
        
        public LoginWindow()
        {
            InitializeComponent();

            WindowStyle = System.Windows.WindowStyle.None;

            SHDocVw.WebBrowser axBrowser = (SHDocVw.WebBrowser)webBrowser.ActiveXInstance;
            axBrowser.NavigateError += new SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(axBrowser_NavigateError);

            webBrowser.Navigate(loginUrl);
        }

        void axBrowser_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
        {
            if (MessageBox.Show("Facebook is currently unavaiable ! Do you want to retry connecting?", "Error", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                webBrowser.Navigate(loginUrl);
            }
            else
            {
                Application.Current.Shutdown();
            }
        }

        private void webBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowserHost.Visibility = System.Windows.Visibility.Visible;

            webBrowser.Document.Focus();

            if (webBrowser.Url.AbsoluteUri.Contains("www.facebook.com/connect/login_success.html"))
            {
                if (!webBrowser.Url.AbsoluteUri.Contains("error"))
                {
                    if (webBrowser.Url.AbsoluteUri.Contains("access_token="))
                    {
                        int idx1 = webBrowser.Url.AbsoluteUri.IndexOf("access_token=");

                        ((App)Application.Current).AccessToken = webBrowser.Url.AbsoluteUri.Split('&')[0];
                        ((App)Application.Current).AccessToken = ((App)Application.Current).AccessToken.Remove(0, idx1 + "access_token=".Length);

                        if (UserLoggedIn != null)
                        {
                            UserLoggedIn(this, EventArgs.Empty);
                        }

                        Close();
                    }
                }
                else
                {
                    webBrowser.Navigate(loginUrl);
                }
            }
        }

        private void webBrowser_Navigating(object sender, System.Windows.Forms.WebBrowserNavigatingEventArgs e)
        {
            webBrowserHost.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}
